home *** CD-ROM | disk | FTP | other *** search
/ PD ROM 1 / PD ROM Volume I - Macintosh Software from BMUG (1988).iso / Stacks / Updates⁄New / TEXAS for BMUG / C progs / brwsr.2 ƒ / brws_utils.2.c next >
Encoding:
C/C++ Source or Header  |  1987-10-08  |  3.6 KB  |  162 lines  |  [TEXT/KAHL]

  1. /* miscellaneous utility functions for helping the higher-level routines
  2.  * browse around in indexed files....
  3.  * 870805-13-...  ^z
  4.  */
  5.  
  6. #include <stdio.h>            /* for FILE, getc(), etc. */
  7. #include <strings.h>        /* for strncpy(), etc. */
  8. #include <ctype.h>            /* for isprint(), etc. */
  9. #include <unix.h>            /* for exit(), time(), etc. */
  10. #include <proto.h>            /* for function prototypes */
  11. #include "brwsr.h"            /* for various definitions */
  12. #include "brwsr.proto.h"    /* for my function prototypes */
  13.  
  14.  
  15.  
  16. /* function to get the 'n'th KEY_REC from key_file and store it in the
  17.  * KEY_REC space pointed to by recp ... note that if an illegal value
  18.  * of 'n' is requested, a 'null' KEY_REC is produced....
  19.  */
  20.  
  21. void get_key_rec (recp, n)
  22.   KEY_REC *recp;
  23.   register long n;
  24.   {
  25.     extern FILE *key_file;
  26.     extern long min_item[], max_item[];
  27.     char *strncpy();
  28.     
  29.     if (n < min_item[INDEX] || n > max_item[INDEX])
  30.       {
  31.         strncpy ((char *)recp->kkey, "                    ",
  32.                     KEY_LENGTH);
  33.         recp->ccount = 0;
  34.         return;
  35.       }
  36.     
  37.     if (fseek (key_file, sizeof (KEY_REC) * n, 0) != 0)
  38.       {
  39.         beep ();
  40.         printf ("Fatal error in fseek() getting key record #%ld!\n", n);
  41.         exit();
  42.       }
  43.     
  44.     if (fread ((char *)recp, sizeof (KEY_REC), 1, key_file) == 0)
  45.       {
  46.         beep ();
  47.         printf ("Fatal error in fread() getting key record #%ld!\n", n);
  48.         exit();
  49.       }
  50.   }
  51.  
  52.  
  53. /* routine to get the nth pointer record (a long integer) from ptr_file;
  54.  * the pointer record gives the actual offset in the document file of
  55.  * the nth word in the expanded index....
  56.  */
  57.  
  58. PTR_REC get_ptr_rec (n)
  59.   register long n;
  60.   {
  61.     PTR_REC p;
  62.     extern FILE *ptr_file;
  63.     
  64.     if (fseek (ptr_file, sizeof (PTR_REC) * n, 0) != 0)
  65.       {
  66.         beep ();
  67.         printf ("Fatal error in fseek() getting pointer record #%ld!\n", n);
  68.         exit();
  69.       }
  70.     
  71.     if (fread ((char *)&p, sizeof (PTR_REC), 1, ptr_file) == 0)
  72.       {
  73.         beep ();
  74.         printf ("Fatal error in fread() getting pointer record #%ld!\n", n);
  75.         exit();
  76.       }
  77.     
  78.     return (p);
  79.   }
  80.  
  81.  
  82. /* Move backwards in document file to find the start of the line of text
  83.  * which has offset p (from start of file) somewhere in that line.
  84.  * However, don't go back beyond STRLEN characters at a time... that
  85.  * is, put a ceiling of STRLEN on the maximum 'length of a line', to
  86.  * avoid pathological input files with no \n's....
  87.  * Do the work by grabbing a buffer full and then scanning back in that,
  88.  * rather than by laboriously lseek() and fgetc() back one character at
  89.  * a time....
  90.  */
  91.  
  92. long start_of_line (p)
  93.   register long p;
  94.   {
  95.     extern FILE *doc_file;
  96.     char buf[STRLEN];
  97.     register long i;
  98.     register int n;
  99.  
  100.     if (p <= 0)
  101.         return (0L);
  102.  
  103.     i = p - STRLEN;
  104.     i = ((i < 0) ? 0 : i);
  105.     if (fseek (doc_file, i, 0) != 0)
  106.       {
  107.         beep ();
  108.         printf ("Fatal error in fseek() backing up in document!\n");
  109.         exit();
  110.       }
  111.     if ((n = fread (buf, sizeof (char), p - i, doc_file)) == 0)
  112.       {
  113.         beep ();
  114.         printf ("Fatal error in fread() backing up in document!\n");
  115.         exit();
  116.       }
  117.  
  118.     while (--n >= 0 && buf[n] != '\n');
  119.     
  120.     return (i + n + 1);
  121.   }
  122.  
  123.  
  124. /* scan forward until reaching the beginning of the next line of text;
  125.  * if at or beyond the end of the file, return a pointer to the last
  126.  * character in the file ...
  127.  * Don't go beyond STRLEN characters, however, in scanning forward....
  128.  */
  129.  
  130. long next_line (p)
  131.   long p;
  132.   {
  133.     register int c, n;
  134.     extern long max_item[];
  135.     
  136.     if (p >= max_item[TEXT])
  137.         return (max_item[TEXT]);
  138.     
  139.     if (fseek (doc_file, p, 0) != 0)
  140.       {
  141.         beep ();
  142.         printf ("Fatal error in fseek() scanning forward in document!\n");
  143.         exit();
  144.       }
  145.     
  146.     for (n = 0; n < STRLEN; ++n)
  147.         if ((c = fgetc (doc_file)) == EOF || c == '\n')
  148.             break;
  149.     
  150.     return (ftell (doc_file));
  151.   }
  152.  
  153.  
  154. /* paging James Bond ....
  155.  */
  156.  
  157. void beep ()
  158.   {
  159.     putchar ('\007');
  160.   }
  161.  
  162.